package style.Leetcode.初级算法.tree.tree_20220827_2_对称二叉树;

import style.Leetcode.初级算法.tree.TreeNode;

/**
 * 给你一个二叉树的根节点 root ， 检查它是否轴对称。
 *
 * 输入：root = [1,2,2,3,4,4,3]
 * 输出：true
 *
 * 输入：root = [1,2,2,null,3,null,3]
 * 输出：false
 */
public class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return isSymmetricLeftAndRight(root.left, root.right);
    }

    public boolean isSymmetricLeftAndRight(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        if (left == null || right == null || left.val != right.val) return false;
        return isSymmetricLeftAndRight(left.left, right.right) && isSymmetricLeftAndRight(left.right, right.left);
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        treeNode.left = new TreeNode(2);
        treeNode.left.left = new TreeNode(3);
        treeNode.left.right = new TreeNode(4);
        treeNode.right = new TreeNode(2);
        treeNode.right.left = new TreeNode(4);
        treeNode.right.right = new TreeNode(3);
        Solution solution = new Solution();
        System.out.println(solution.isSymmetric(treeNode));
    }
}
